<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="com.shsh.safemanage.model.JobPost"%>
<%@page import="com.shsh.safemanage.dao.TitlePostManageGroupDao"%>
<%@page import="java.util.Calendar"%>
<%@page import="com.shsh.safemanage.dao.DepartmentDao"%>
<%@page import="com.shsh.safemanage.dao.PostCheckDao"%>
<%@page import="com.shsh.safemanage.model.Department1"%>
<%@page import="com.shsh.safemanage.model.PostCheck"%>
<%@page import="com.shsh.safemanage.model.PostcheckPattern"%>
<%@page import="com.shsh.safemanage.model.PostcheckContent"%>
<%@page import="com.shsh.safemanage.model.PostcheckStandard"%>
<%@page import="com.shsh.safemanage.model.ProblemTransfer"%>
<%@page import="java.util.List"%>
<%@page import="com.shsh.safemanage.util.Util"%>
<%@page import="com.shsh.safemanage.model.Problem"%>
<%@page import="com.shsh.safemanage.dao.DaoCreator"%>
<%@page import="com.shsh.safemanage.dao.ProblemDao"%>
<%@page import="com.shsh.safemanage.util.Constants"%>

<%
session.removeAttribute(Constants.SESSION_MANAGE_GROUP_USER);
session.removeAttribute(Constants.SESSION_PROBLEM_DEPARTMENT1);

Long postcheckStandardID = 0l;
String postcheckStandardIDStr = (String)request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_STANDARD_ID);
try{
	postcheckStandardID = Long.parseLong(postcheckStandardIDStr);
} catch (Exception e){
	
}
String problemIDStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
Long problemID = 0L;
try {
	problemID = new Long(problemIDStr);
} catch (Exception e) {
}

String transferIDStr = request.getParameter(Constants.REQUEST_PARAMETER_TRANSFER_ID);
Long transferID = 0L;
try {
	transferID = new Long(transferIDStr);
} catch (Exception e) {
}

DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
ProblemDao problemDao = DaoCreator.createProblemDao();
PostCheckDao postcheckDao = DaoCreator.createPostCheckDao();

PostcheckStandard postcheckStandard = postcheckDao.getPostcheckStandardByID(postcheckStandardID);
PostcheckContent postcheckContent = postcheckDao.getPostcheckContentByID(postcheckStandard.getPostcheckContentID());
String fromPage = request.getParameter(Constants.REQUEST_PARAMETER_FROM_PAGE);


TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();
WriteProblemAction writeProblemAction = ActionCreator.createWriteProblemAction();
UserAction userAction = ActionCreator.createUserAction();

Problem problem = problemDao.getProblemByID(problemID);

PostCheck postcheck = null;
PostcheckPattern postcheckPattern = null;

if(problem.getProblemType() == Constants.PROBLEM_TYPE_POSTCHECK) {
	postcheck = postcheckDao.getPostCheck(problem.getPostcheckID());
	postcheckPattern = postcheckDao.getPostcheckPatternByID(problem.getPostcheckPatternID());
	postcheckContent =  postcheckDao.getPostcheckContentByID(problem.getPostcheckContentID());
	postcheckStandard = postcheckDao.getPostcheckStandardByID(problem.getPostcheckStandardID());
}

String initDateEndStr = Util.generateDateString(3, "yyyy-MM-dd");

User user = (User) session.getAttribute(Constants.SESSION_USER);
JobPost post1 = titlePostManageGroupDao.getJobPostByID(user.getJobPost1ID());
JobPost post2 = titlePostManageGroupDao.getJobPostByID(user.getJobPost2ID());
JobPost post3 = titlePostManageGroupDao.getJobPostByID(user.getJobPost3ID());
Role theRole = userAction.getUserRole(request);

int role = writeProblemAction.calRole(post1, post2, post3, theRole);
%>
<div class="hider" id="hider">
	<span>&nbsp;</span>
</div>
<div class="position">
	<img src="images/position2.jpg" style="padding: 7px 9px; float: left;" />当前位置：上海高桥分公司问题管理平台 &gt;&gt; 处理问题
</div>

<div class="tt"><%=postcheckContent.getContent() %>岗检填报卡</div>
<div class="content">
	<table align="center" border="1" width="99%" cellpadding="0"
		cellspacing="0">
		<tr>
			<td class="td1"><span
				style="margin-left: 30px; font-weight: bold">作业部（业务中心）：</span> <span><%=problem.getDepartment1Name()%></span>
				<span style="margin-left: 30px; font-weight: bold">装置：</span> <span><%=problem.getEquipmentName()%></span>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<span style="margin-left: 40px" class="span1">发现问题标题：（即：填写HSE观察作业卡上的“区域/设施”+“观察的作业”）</span>
				<br />
				<input type="text" style="margin-left: 60px" id="problemTitle" size="45" value="<%=problem.getProblemTitle() %>" />
				<input type="checkbox" id="isRegular" name="isRegular" style="margin-left: 10px" onclick="setIsRegular()" />
				<label for="isRegular" style="margin-left: 3px">检查正常</label>
				<input type="checkbox" id="isSanwei" name="isSanwei" style="margin-left: 10px" onclick="setIsSanwei()" />
				<label for="isSanwei" style="margin-left: 3px">“三违”现象</label>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<span style="margin-left: 40px" class="span1">发现问题内容：（即：HSE观察作业卡上的“观察事实描述”）</span>
				<br />
				<textarea style="width: 600px; height: 100px; margin-left: 60px" id="problemContent"><%=problem.getProblemContent() %></textarea>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<span style="margin-left: 40px" class="span1">整改措施：（即：HSE观察作业卡上的“立即采取的措施”）</span>
				<br />
				<textarea style="width: 600px; height: 100px; margin-left:60px" id="zhenggai_cuoshi"><%=problem.getProblemZhenggai() %></textarea>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<div style="margin-left:40px;margin-right:40px;">工作标准与要求：<%=postcheckStandard.getStandard()%></div>			
				<span style="margin-left: 40px" class="span1">扣分值：</span><input type="text" id="grade" name="grade" value="0"/>
				<span style="margin-left: 20px" class="span1">本条工作标准中的扣分标准：</span><input type="text" id="gradeStandard" name="gradeStandard" value="<%=postcheckStandard.getGradeStandard()%>" readonly="readonly"/>
				<br />
				<span style="margin-left: 40px" class="span1">要求整改完成日期：</span><input type="text" id="completeDate" size="15" 
					onfocus="WdatePicker({maxDate:'#F{\'2050-12-31\'}'})" value="<%=initDateEndStr%>"/>
			</td>
		</tr>
		<tr id="informedPersonsTR">
			<td class="td1">
				<%
				if ((role & Constants.ROLE_TYPE_ZUZHANG) > 0) {
				%>
				<div id="informPersonDiv">
				<%@include file="/problem_feedback_informed_person.jsp" %>
				</div>
				<%
				}
				%>
				<%
				if ((role & (Constants.ROLE_TYPE_ZHUANGZHIZHANG | Constants.ROLE_TYPE_ZUZHANG)) > 0) {
				%>
				<div id="chooseManageGroupUserDiv">
				<%@include file="/managegroup_select/manage_check_samepage_section.jsp"%>
				</div>
				<%
				}
				%>
				<%
				if ((role & (Constants.ROLE_TYPE_KEYUAN | Constants.ROLE_TYPE_KEZHANG | Constants.ROLE_TYPE_FUKEZHANG 
						| Constants.ROLE_TYPE_FUCHUZHANG | Constants.ROLE_TYPE_CHUZHANG)) > 0) {
				%>
				<div id="chooseChushiTransferDiv">
				<%@include file="/chushi_informed_person.jsp"%>
				</div>
				<%
				}
				%>
			</td>
		</tr>
		<tr>
			<td class="td1">
				<span style="margin-left: 30px" class="span1">填报人及时间：</span>
				<span><%=user.getDepartment1Name()%>-<%=user.getDepartment2Name()%>-<%=user.getDepartment3Name()%></span>
				<span><%=user.getName()%></span>
				<span><%=Util.formatDateTime(Calendar.getInstance())%></span>
			</td>
		</tr>
		<tr>
			<td class="td1" align="center">
				<% if(!"postcheckReport".equals(fromPage)) {%>
				<input type="button" value="转化" onclick="convertProblem()" />
				<%} %>
				<input type="button" value="取 消" style="margin-left: 20px" onclick="cancelConvert()" />
			</td>
		</tr>
	</table>
</div>

<script type="text/javascript">

function convertProblem() {	
	var problemContent = getElementValueByID('problemContent');
	if (problemContent == null || problemContent.length < 5) {
		$.dialog.alert('在发现问题内容一栏里必需填报岗检情况描述');
		return;
	}
	
	var grade = getElementValueByID('grade');
	if(grade == null ||  grade == '' || grade == 'undefined') {
		$.dialog.alert('请在扣分值输入一个数字');
		return;
	}
	
	var strExp = <%=Constants.REGX_GRADE%>;
	if (!strExp.test(grade)){
		$.dialog.alert('扣分值必须为非负数');
		return;
	}
	
	var isRegular = 0;
	if (document.getElementById('isRegular').checked) {
		isRegular = 1;
	}
	
	if ($.dialog.confirm('您确定转换此问题吗？', function(){
		openWaitingDialog('waitingDialog');
		$.ajax({
			type:'post',
			url:'<%=request.getContextPath()%>/postcheck/convert_problem_process.jsp',
			data:{
				isRegular: isRegular,
				isSanwei: $('#isSanwei').attr('checked'),
				problemID: <%=problemID%>,
				problemTitle: getElementValueByID('problemTitle'),
				problemContent: getElementValueByID('problemContent'),
				zhenggai_cuoshi: getElementValueByID('zhenggai_cuoshi'),
				postcheckID: <%=postcheckStandard.getPostcheckID()%>,
				postcheckPatternID: <%=postcheckStandard.getPostcheckPatternID()%>,
				postcheckContentID: <%=postcheckStandard.getPostcheckContentID()%>,
				postcheckStandardID: <%=postcheckStandard.getId()%>,
				grade: grade,
				completeDate: getElementValueByID('completeDate')
			},
			success:function(data,state,xmlhttp){
				var obj = null;
				try {
					obj = eval('(' + data + ')');
				} catch (e) {
				}
				if (typeof(obj.isSuccess) != 'undefined') {
					closeWaitingDialog('waitingDialog');
					if (obj.isSuccess == 'true') {
						$.dialog('问题转换成功！', function() {
							loadPageSection('<%=request.getContextPath()%>/postcheck/postcheck_standard_list.jsp', 'mainContentDiv', {postcheckID: <%=postcheckStandard.getPostcheckID()%>}, true); 
						});
					} else {
						$.dialog.alert(obj.error);
					}
				}
			}
		});
	}, function(){}));
}

function cancelConvert() {
	$.dialog.confirm('您确定取消此次转换吗？', function() {
		loadPageSection(
				'<%=request.getContextPath()%>/postcheck/convert_problem_postcheck_entrance.jsp', 
				'mainContentDiv',
				{
					problemID: <%=problemID%>, 
					postcheckID: <%=postcheckStandard.getPostcheckID()%>
				}, 
			true);
		
	}, function(){});
}

function setIsRegular(){
	if($('#isSanwei').attr('checked') == true){
		$.dialog.alert('已选择“三违”现象');
		$('#isRegular').attr('checked', false);
		return;
	}
	var isRegularElement = document.getElementById('isRegular');
	if (isRegularElement.checked) {
		var content = getElementValueByID('problemContent');
		if(content == null || content == '') {
			setElementValueByID('problemContent', content + '经检查正常');
		} else {
			setElementValueByID('problemContent', content + '\r经检查正常');
		}
		var zhenggai = getElementValueByID('zhenggai_cuoshi');	
		if(zhenggai == null || zhenggai == '') {
			setElementValueByID('zhenggai_cuoshi', zhenggai + '检查正常');
		} else {
			setElementValueByID('zhenggai_cuoshi', zhenggai + '\r检查正常');
		}
		$("#grade").attr("readonly", "readonly");
	} else {
		setElementValueByID('problemContent', '');
		setElementValueByID('zhenggai_cuoshi', '');
		$("#grade").attr("readonly", "");
	}
}

function setIsSanwei(){
	if($('#isRegular').attr('checked') == true){
		$.dialog.alert('已选择检查正常');
		$('#isSanwei').attr('checked', false);
		return;
	}
}

function chooseProcessMethod(method) {
	if (method == 1) {
		document.getElementById('informedPersonsTR').style.display = 'none';
		document.getElementById('feedback').readonly = true;
		setElementValueByID('feedback', '<%=user.getDepartment1Name()%>，<%=user.getJobPost1Name()%><%=user.getName()%>确认该问题已整改');
	} else if (method == 2) {
		document.getElementById('informedPersonsTR').style.display = '';
		document.getElementById('feedback').readonly = false;
		setElementValueByID('feedback', '');
	} else if (method == 4 || method == 3 || method == 5 || method == 6 || method == 0) {
		document.getElementById('informedPersonsTR').style.display = 'none';
		setElementValueByID('feedback', '');
	}
}
</script>
